package com.itmd.dao;

import com.itmd.domain.Order;
import com.itmd.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface OrderMapper {

    @Select("select * from orders where uid=#{id}")
    List<Order> findOrdersByUid(Integer id);


    @Insert("insert into orders values (#{id},#{ordertime},#{user.id},#{total});")
    void addOrder(Order order);


    //两张表分别查询，再分别封装
    @Select("select * from orders")
    @Results(value = {
            @Result(column = "id",property = "id"),
            @Result(column = "ordertime",property = "ordertime"),
            @Result(column = "total",property = "total"),
            @Result(
                    property = "user",  // 要封装的属性
                    javaType = User.class, //要封装的属性的类型
                    column = "uid" ,//通过字段查询要封装的user
                    one = @One(select = "com.itmd.dao.UserMapper.findById")
            )
    })
    List<Order> findAll2();

    //两张表同时查询
    @Select("select *,o.id oid from orders o,user u where o.uid=u.id")
    @Results({
            @Result(column = "oid",property = "id"),
            @Result(column = "ordertime",property = "ordertime"),
            @Result(column = "total",property = "total"),
            @Result(column = "uid",property = "user.id"),
            @Result(column = "username",property = "user.username"),
            @Result(column = "password",property = "user.password")
    })
    List<Order> findAll();


    @Select("select * from orders o")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "ordertime",property = "ordertime"),
            @Result(column = "total",property = "total"),
    })
    List<Order> findAllorders();
}
